package com.zy.multiredis.config;

import java.util.Map;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

@Configuration
@ConditionalOnClass(MultiRedisTemplateMap.class)
public class MultiRedisAutoConfig {
    
    @Bean("redisTemplate")
    @Primary
    @ConditionalOnMissingBean(RedisTemplate.class)
    public RedisTemplate configDefaultRedisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        return template;
    }
    
    @Bean
    public MultiRedisTemplateMap configTemplate(Map<String,RedisTemplate> redisTemplates) {
        MultiRedisTemplateMap map = new MultiRedisTemplateMap(redisTemplates);
        return map;
    }
    
//  @Bean("redis2")
//  public RedisTemplate configRedisTemplate2(multiRedisConnectionFactoryMap connectionFactoryMap) {
//      RedisTemplate template = new RedisTemplate<>();
//      template.setConnectionFactory(connectionFactoryMap.getRedisConnectionFactories().get("redis2"));
//      return template;
//  }
}
